package com.rupertjones.globalcron.web.service;

import com.rupertjones.globalcron.domain.Setting;
import com.rupertjones.globalcron.domain.SettingDAO;
import com.rupertjones.globalcron.domain.SettingType;
import com.rupertjones.globalcron.util.TypeConverter;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Service
@Transactional
public class DefaultSettingsService implements SettingsService {

    private static final Logger LOG = Logger.getLogger(DefaultSettingsService.class);

    @Autowired
    private SettingDAO settingDAO;

    @Override
    public Setting find(SettingType type) {
        return settingDAO.findBy(type);
    }

    @Override
    public Setting find(int settingId) {
        return settingDAO.findById(settingId);
    }

    @Override
    public Collection<Setting> findAll() {
        return settingDAO.findAll();
    }

    @Override
    public Object getSettingValue(SettingType type) {
        Setting setting = find(type);
        return TypeConverter.convert(setting.getValue(), setting.getValueTypeAsClass());
    }

    @Override
    public void save(Setting setting) {
        settingDAO.upsert(setting);
    }
}
